#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 胡浩浩
# datetime： 2021/12/28 15:44 
# ide： PyCharm
from sqlalchemy.orm import Session
from Models.apiModel import api_model
from Schemas.apiSchema import api_schema


def get_api_by_id(db: Session, api_id: int):
    return db.query(api_model.Api).filter(api_model.Api.id == api_id).first()


def get_api_by_path(db: Session, path: str):
    return db.query(api_model.Api).filter(api_model.Api.name == path).first()


def get_api_list(db: Session, skip: int = 0, limit: int = 10):
    return db.query(api_model.Api).offset(skip).limit(limit).all()


def create_api(db: Session, api: api_schema.ApiCreate):
    db_api = api_model.Api(**api.dict())
    db.add(db_api)
    db.commit()
    db.refresh(db_api)
    return db_api
